#pragma once
#include<assert.h>

namespace zpl
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin() const
		{
			return _start;
		}
		const_iterator end() const
		{
			return _finish;
		}
		size_t size() const
		{
			return _finish - _start;
		}
		size_t capacity() const
		{
			return _endofstorage - _start;
		}
		void reserve(size_t n)
		{
			if (n > capacity())
			{	
				size_t old = size();
				T* tmp = new T[n];
				if (_start)
				{
					memcpy(tmp, _start, sizeof(T) * old);
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + old;
				_endofstorage = _start + n;
			}
		}
		void resize(size_t n, T val = T());
		void erase(iterator pos);
		void insert(iterator pos, const T& x)
		{
			assert(_start <= pos && pos <= _finish);
			size_t len = pos - _start;
			if (size() == capacity())
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}
			pos = _start + len;
			memmove(pos + 1, pos, sizeof(T) * (_finish - pos));
			*pos = x;
			_finish++;
		}
		void push_back(const T& x)
		{
			if (size() == capacity())
			{
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
			}
			*_finish = x;
			_finish++;
		}
		void pop_back()
		{
			assert(size() > 0);
			_finish--;
		}
		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}
		const T& operator[](size_t pos) const
		{
			assert(pos < size());
			return _start[pos];
		}
	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _endofstorage = nullptr;
	};
	void print_vector(const vector<int>& v)
	{
		for (auto& e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}
	void testVector1()
	{
		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		print_vector(v1);
		v1.insert(v1.begin(), 6);
		print_vector(v1);
	}
}